/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  MarketController.java   
 * @Package com.axin.controller   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年3月11日 下午10:37:10   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.axin.common.entity.ApiToken;
import com.axin.common.entity.JsonResult;
import com.axin.common.entity.response.Merged;
import com.axin.common.entity.response.MergedResponse;
import com.axin.common.exception.ApiException;
import com.axin.service.MarketService;

/**   
 * @Description: 市场行情 
 * @ClassName:  MarketController
 * @author:  Axin 
 * @date:   2019年3月11日 下午10:37:10   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */

@Controller
@RequestMapping("/market")
public class MarketController {
	
	@Autowired
	private MarketService marketService;
	
	@Value("${ACCESS_KEY_ID}")
	private String ACCESS_KEY_ID;
	@Value("${ACCESS_KEY_SECRET}")
	private String ACCESS_KEY_SECRET;
	@Value("${ACCESS_PASSWORD}")
	private String ACCESS_PASSWORD;
	
	@RequestMapping("/getKline")
	@ResponseBody
	public JsonResult getKline(String symbol, String period, String size,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new RuntimeException("您尚未设置密钥，不能处理您的请求！"));
		}
		//ApiToken token = new ApiToken(ACCESS_KEY_ID,ACCESS_KEY_SECRET);
		if(StringUtils.isEmpty(symbol))symbol = "btcusdt";
		if(StringUtils.isEmpty(period))period = "5min";
		if(StringUtils.isEmpty(size))size = "100";
		 Map<String,String> map = new HashMap();
		 map.put("symbol", symbol);
	     map.put("period", period);
	     map.put("size", size);
		return this.marketService.getKline(token, map);
	}
	
	
	/**
	 * 获取货币对的聚合行情
	 * @Title: getDetailMerged   
	 * @param symbol
	 * @param request
	 * @return JsonResult
	 * @throws
	 */
	@RequestMapping("/getDetailMerged")
	@ResponseBody
	public JsonResult getDetailMerged(@RequestParam(value="symbol",required=true)String symbol,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new RuntimeException("您尚未设置密钥，不能处理您的请求！"));
		}
		Map<String,String> map = new HashMap();
		 map.put("symbol", symbol);
		try {
			Merged merged = this.marketService.getDetailMerged(token, map);
			return new JsonResult(merged);
		} catch (ApiException e) {
			String errCode = e.getErrCode();
			 e.printStackTrace();
			 return new JsonResult(new RuntimeException("接口请求异常，错误码："+errCode + ",错误消息："+e.getMessage()));
		} catch (Exception e) {
			 e.printStackTrace();
			 return new JsonResult(new RuntimeException("请求异常"));
		}
		
		 
	}
	
}



